package com.seirsoft.cookingtime.ui.dialog;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import com.seirsoft.cookingtime.internal.ICtlBase;
import com.seirsoft.cookingtime.view.IDlgBase;

public abstract class DialogBase extends Dialog 
implements IDlgBase, SelectionListener
{
	protected ICtlBase m_ctlBase = null;
	
	// RETURN
	protected int m_iDialogReturnValue = -1;
	
	public DialogBase(Shell parent) {
		super(parent);
		super.setReturnCode(SWT.CANCEL);
	}
	
	@Override
	public void setController(ICtlBase ctlBase)
	{
		m_ctlBase = ctlBase;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		
		this.m_ctlBase.fireEventUIPreCreate();
		this.createUI(parent);	
		this.addListener();
		this.m_ctlBase.fireEventUICreated();

		return parent;
	}
	
	@Override
	public int getDialogReturnValue()
	{
		return m_iDialogReturnValue;
	}
	
	@Override
	public void openDialog() {
		m_iDialogReturnValue = this.open();
	}
	
	@Override
	protected void handleShellCloseEvent() {
		this.m_ctlBase.fireEventDialogClosed();
		super.handleShellCloseEvent();
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
    }
	
	protected Button createButton(Composite parent, String strLabel)
	{
		GridData layoutData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER | GridData.CENTER);
		layoutData.grabExcessHorizontalSpace = true;
		layoutData.widthHint = 80;

		Button btnTemp = new Button(parent, SWT.None);
		btnTemp.setText(strLabel);
		btnTemp.setLayoutData(layoutData);
		
		return btnTemp;
	}
	
	@Override
	public void setTitle(String dialogTitle) {
		super.getShell().setText(dialogTitle);
	}
	
	@Override
	public void setSize(int x, int y)
	{
		super.getShell().setSize(new Point(x, y));
	}
	
	@Override
	public void widgetDefaultSelected(SelectionEvent evt) {
		
	}

	@Override
	public void widgetSelected(SelectionEvent evt) {
		fireSelectedEvent(evt);
	}
	
	
	@Override
	public void closeDialog(boolean isOK)
	{
		if(isOK)
			super.okPressed();
		else
			super.cancelPressed();
	}
	
	@Override
	public boolean isDisposed()
	{
		return this.getShell() == null || this.getShell().isDisposed();
	}
	
	@Override
	public void showErrorMessage(String strMessage)
	{
		if(!this.isDisposed())
		{
			Shell shell = this.getShell();
			MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
			box.setText(shell.getText());
			box.setMessage(strMessage);
			box.open();
		}
	}
	
	@Override
	public void showInfoMessage(String strMessage)
	{
		if(!this.isDisposed())
		{
			Shell shell = this.getShell();
			MessageBox box = new MessageBox(shell, SWT.ICON_INFORMATION);
			box.setText(shell.getText());
			box.setMessage(strMessage);
			box.open();
		}
	}
	
	protected abstract void createUI(Composite parent);
	protected abstract void addListener();
	protected abstract void fireSelectedEvent(SelectionEvent evt);
}
